package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.JsolaException;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.PositionInfoAddDTO;
import org.jsola.hr.dto.PositionInfoUpdateDTO;
import org.jsola.hr.entity.CompanyDO;
import org.jsola.hr.query.PositionInfoQuery;
import org.jsola.hr.service.ICompanyService;
import org.jsola.hr.service.IPositionInfoService;
import org.jsola.hr.vo.EmpAllInfoVO;
import org.jsola.hr.vo.PositionInfoListVO;
import org.jsola.hr.vo.PositionInfoVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;


/**
 * @author zhr
 */
@RestController("hrPositionInfoController")
@RequestMapping(HrConstants.API_PATH + "/v1/positionInfos")
public class PositionInfoController implements IPositionInfoControllerAPI {

    @Autowired
    private IPositionInfoService positionInfoService;

    @Autowired
    private ICompanyService companyService;

    @Override
    @PostMapping
    public Result<PositionInfoVO> save(@Valid @RequestBody PositionInfoAddDTO positionInfoAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增岗位信息权限校验

        return Result.success(
                positionInfoService.save(positionInfoAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody PositionInfoUpdateDTO positionInfoUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改岗位信息权限校验

        int count = positionInfoService.update(positionInfoUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{positionInfoId}")
    public Result<Integer> delete(@PathVariable String positionInfoId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除岗位信息权限校验

        int count = positionInfoService.deleteByIds(tokenUser, positionInfoId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] positionInfoIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除岗位信息权限校验

        int count = positionInfoService.deleteByIds(tokenUser, positionInfoIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{positionInfoId}")
    public Result<PositionInfoVO> findById(@PathVariable String positionInfoId,
                                           @RequestParam("companyId") String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看岗位信息权限校验

        PositionInfoVO positionInfoVO = positionInfoService.selectById(positionInfoId, tokenUser.getSiteId());
        if (Objects.nonNull(positionInfoVO) && !companyId.equals(positionInfoVO.getCompanyId())) {
            return null;
        }
        return Result.success(positionInfoVO);
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<PositionInfoListVO>> listAll(@Valid PositionInfoQuery positionInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看岗位信息权限校验

        // 扫码入职 查询岗位时用户未登录
        if (tokenUser == null) {
            tokenUser = new TokenUser();
            CompanyDO companyDO = companyService.selectDOByIdNotSite(positionInfoQuery.getCompanyId());
            ParamException.notNull(companyDO, "用工单位不存在，请刷新后重试");
            tokenUser.setSiteId(companyDO.getSiteId());
        }

        return Result.success(
                positionInfoService.select(positionInfoQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<PositionInfoListVO>> page(@Valid PositionInfoQuery positionInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                positionInfoService.selectPage(positionInfoQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/getPositionInfoListPage")
    public Result<Page<PositionInfoListVO>> getPositionInfoListPage(@Valid PositionInfoQuery positionInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                positionInfoService.selectPositionInfoListPage(positionInfoQuery, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/listPositionType/{companyId}")
    public Result<List<PositionInfoListVO>> listPositionTypeByCompanyId(@PathVariable String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                positionInfoService.listPositionTypeByCompanyId(companyId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listTree")
    public Result<List<PositionInfoListVO>> listTree(@Valid PositionInfoQuery positionInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看部门信息权限校验

        return Result.success(
                positionInfoService.listTree(positionInfoQuery, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/findByEmpId/{empInfoId}")
    public Result<EmpAllInfoVO> findByEmpId(@PathVariable String empInfoId,
                                            @RequestParam("companyId") String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看岗位信息权限校验
        EmpAllInfoVO empAllInfoVO = positionInfoService.selectByEmpId(empInfoId, tokenUser.getSiteId());
        if (Objects.nonNull(empAllInfoVO) && !companyId.equals(empAllInfoVO.getCompanyId())) {
            return null;
        }
        return Result.success(empAllInfoVO);
    }


}

